package cn.jeeweb.web.modules.equipment.controller;

import cn.jeeweb.common.http.Response;
import cn.jeeweb.common.http.ResponseCode;
import cn.jeeweb.common.mvc.annotation.ViewPrefix;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.web.aspectj.annotation.Log;
import cn.jeeweb.web.modules.equipment.entity.EqsFixture;
import cn.jeeweb.web.modules.equipment.entity.EqsOperateApply;
import cn.jeeweb.web.modules.equipment.entity.EqsSpareOperateApply;
import cn.jeeweb.web.modules.equipment.entity.EqsSparePart;
import cn.jeeweb.web.modules.equipment.service.IEqsFixtureService;
import cn.jeeweb.web.modules.equipment.service.IEqsOperateApplyService;
import cn.jeeweb.web.modules.equipment.service.IEqsSpareOperateApplyService;
import cn.jeeweb.web.modules.equipment.service.IEqsSparePartService;
import cn.jeeweb.web.modules.sys.Constants;
import cn.jeeweb.web.modules.sys.entity.UserRole;
import cn.jeeweb.web.modules.sys.service.IUserRoleService;
import cn.xjzn.framework_Core.ModuleActionType;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/eqs/operateapply")
@ViewPrefix("")
//@RequiresPathPermission("sys:location")
@Log(title = "eqs_operate_apply")
public class EqsOperateApplyController {

    @Autowired
    private IEqsOperateApplyService eqsOperateApplyService;

    @Autowired
    private IEqsSpareOperateApplyService eqsSpareOperateApplyService;

    @Autowired
    private IUserRoleService userRoleService;

    @Autowired
    private IEqsFixtureService eqsFixtureService;

    @Autowired
    private IEqsSparePartService eqsSparePartService;

    @RequestMapping(value = "getAuditByUserId", method = {RequestMethod.GET, RequestMethod.POST})
    public Response getAuditByUserId(String userId, String buttonCode) {
        String roleId = "";
        EntityWrapper<UserRole> entityWrapper = new EntityWrapper<>(UserRole.class);
        entityWrapper.eq("userId", userId);
        List<UserRole> userRoleList = userRoleService.selectList(entityWrapper);
        if (userRoleList != null && userRoleList.size() > 0) {
            roleId = userRoleList.get(0).getRoleId();
        }
        if (ModuleActionType.UPDATE.toString().equals(buttonCode)) {
            if (Constants.AUDIT_ROLE.equals(roleId)) {
                return Response.error(ResponseCode.NO_AUTHORITY, "没有权限进行修改！");
            }
        } else if (ModuleActionType.AUDITOPERATEAPPLY.toString().equals(buttonCode)) {
            if (Constants.OPERATE_ROLE.equals(roleId)) {
                if (Constants.AUDIT_ROLE.equals(roleId)) {
                    return Response.error(ResponseCode.NO_AUTHORITY, "没有权限进行修改！");
                }
            }
        }
        return Response.ok("请继续操作。");
    }

    /**
     * 入库
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "beLaidUp", method = {RequestMethod.GET, RequestMethod.POST})
    public Response beLaidUp(String id) {
        EntityWrapper<EqsOperateApply> entityWrapper = new EntityWrapper<>(EqsOperateApply.class);
        entityWrapper.eq("id", id);
        EqsOperateApply eqsOperateApply = eqsOperateApplyService.selectOne(entityWrapper);
        if (eqsOperateApply != null) {
            if (Constants.IN_OPERATEAPPLYTYPE.equals(eqsOperateApply.getApplyType()) && Constants.AGREE_AUDIT.equals(eqsOperateApply.getAuditStatus())) {
                Integer applyNumber = eqsOperateApply.getApplyNumber();
                EntityWrapper<EqsFixture> entityWrapper_ = new EntityWrapper<>(EqsFixture.class);
                entityWrapper_.eq("code", eqsOperateApply.getFixtureCode());
                EqsFixture eqsFixture = eqsFixtureService.selectOne(entityWrapper_);
                if (eqsFixture != null) {
                    eqsFixture.setWarehouseCount((eqsFixture.getWarehouseCount() == null ? 0 : eqsFixture.getWarehouseCount()) + applyNumber);
                    eqsFixtureService.insertOrUpdate(eqsFixture);
                    eqsOperateApply.setAuditStatus("4");
                    eqsOperateApplyService.insertOrUpdate(eqsOperateApply);
                } else {
                    return Response.error("工装信息异常！");
                }
            } else {
                return Response.error("该申请不是入库申请或者审核未通过或者操作已完成！");
            }
        } else {
            return Response.error("申请信息异常！");
        }
        return Response.ok("操作成功");
    }

    /**
     * 备件 入库
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "spareBeLaidUp", method = {RequestMethod.GET, RequestMethod.POST})
    public Response spareBeLaidUp(String id) {
        EntityWrapper<EqsSpareOperateApply> entityWrapper = new EntityWrapper<>(EqsSpareOperateApply.class);
        entityWrapper.eq("id", id);
        EqsSpareOperateApply eqsSpareOperateApply = eqsSpareOperateApplyService.selectOne(entityWrapper);
        if (eqsSpareOperateApply != null) {
            if (Constants.IN_SPAREOPERATEAPPLYTYPE.equals(eqsSpareOperateApply.getApplyType()) && Constants.AGREE_AUDIT.equals(eqsSpareOperateApply.getAuditStatus())) {
                Integer applyNumber = eqsSpareOperateApply.getApplyNumber();
                EntityWrapper<EqsSparePart> entityWrapper_ = new EntityWrapper<>(EqsSparePart.class);
                entityWrapper_.eq("code", eqsSpareOperateApply.getSparePartCode());
                EqsSparePart eqsSparePart = eqsSparePartService.selectOne(entityWrapper_);
                if (eqsSparePart != null) {
                    eqsSparePart.setWarehouseCount((eqsSparePart.getWarehouseCount() == null ? 0 : eqsSparePart.getWarehouseCount()) + applyNumber);
                    eqsSparePartService.insertOrUpdate(eqsSparePart);
                    eqsSpareOperateApply.setAuditStatus("4");
                    eqsSpareOperateApplyService.insertOrUpdate(eqsSpareOperateApply);
                } else {
                    return Response.error("备件信息异常！");
                }
            } else {
                return Response.error("该申请不是入库申请或者审核未通过或者操作已完成！");
            }
        } else {
            return Response.error("申请信息异常！");
        }
        return Response.ok("操作成功");
    }


    /**
     * 出库
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "outOfStock", method = {RequestMethod.GET, RequestMethod.POST})
    public Response outOfStock(String id) {
        EntityWrapper<EqsOperateApply> entityWrapper = new EntityWrapper<>(EqsOperateApply.class);
        entityWrapper.eq("id", id);
        EqsOperateApply eqsOperateApply = eqsOperateApplyService.selectOne(entityWrapper);
        if (eqsOperateApply != null) {
            if (Constants.RECEIVE_OPERATEAPPLYTYPE.equals(eqsOperateApply.getApplyType()) && Constants.AGREE_AUDIT.equals(eqsOperateApply.getAuditStatus())) {
                Integer applyNumber = eqsOperateApply.getApplyNumber();
                EntityWrapper<EqsFixture> entityWrapper_ = new EntityWrapper<>(EqsFixture.class);
                entityWrapper_.eq("code", eqsOperateApply.getFixtureCode());
                EqsFixture eqsFixture = eqsFixtureService.selectOne(entityWrapper_);
                if (eqsFixture != null) {
                    int count = (eqsFixture.getWarehouseCount() == null ? 0 : eqsFixture.getWarehouseCount()) - applyNumber;
                    if (count < 0) {
                        return Response.error("库存不足");
                    }
                    eqsFixture.setWarehouseCount(count);
                    eqsFixtureService.insertOrUpdate(eqsFixture);
                    eqsOperateApply.setAuditStatus("4");
                    eqsOperateApplyService.insertOrUpdate(eqsOperateApply);
                } else {
                    return Response.error("工装信息异常！");
                }
            } else {
                return Response.error("该申请不是领用申请或者审核未通过或者操作已完成！");
            }
        } else {
            return Response.error("申请信息异常！");
        }
        return Response.ok("操作成功");
    }


    /**
     * 备件 出库
     * @param id
     * @return
     */
    @RequestMapping(value = "spareOutOfStock", method = {RequestMethod.GET, RequestMethod.POST})
    public Response spareOutOfStock(String id) {
        EntityWrapper<EqsSpareOperateApply> entityWrapper = new EntityWrapper<>(EqsSpareOperateApply.class);
        entityWrapper.eq("id", id);
        EqsSpareOperateApply eqsSpareOperateApply = eqsSpareOperateApplyService.selectOne(entityWrapper);
        if (eqsSpareOperateApply != null) {
            if (Constants.OUT_SPAREOPERATEAPPLYTYPE.equals(eqsSpareOperateApply.getApplyType()) && Constants.AGREE_AUDIT.equals(eqsSpareOperateApply.getAuditStatus())) {
                Integer applyNumber = eqsSpareOperateApply.getApplyNumber();
                EntityWrapper<EqsSparePart> entityWrapper_ = new EntityWrapper<>(EqsSparePart.class);
                entityWrapper_.eq("code", eqsSpareOperateApply.getSparePartCode());
                EqsSparePart eqsSparePart = eqsSparePartService.selectOne(entityWrapper_);
                if (eqsSparePart != null) {
                    int count = (eqsSparePart.getWarehouseCount() == null ? 0 : eqsSparePart.getWarehouseCount()) - applyNumber;
                    if (count < 0) {
                        return Response.error("库存不足");
                    }
                    eqsSparePart.setWarehouseCount(count);
                    eqsSparePartService.insertOrUpdate(eqsSparePart);

                    eqsSpareOperateApply.setAuditStatus("4");
                    eqsSpareOperateApplyService.insertOrUpdate(eqsSpareOperateApply);
                } else {
                    return Response.error("备件信息异常！");
                }
            } else {
                return Response.error("该申请不是领用申请或者审核未通过或者操作已完成！");
            }
        } else {
            return Response.error("申请信息异常！");
        }
        return Response.ok("操作成功");
    }

    /**
     * 报修
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "repair", method = {RequestMethod.GET, RequestMethod.POST})
    public Response repair(String id) {
        EntityWrapper<EqsOperateApply> entityWrapper = new EntityWrapper<>(EqsOperateApply.class);
        entityWrapper.eq("id", id);
        EqsOperateApply eqsOperateApply = eqsOperateApplyService.selectOne(entityWrapper);
        if (eqsOperateApply != null) {
            if (Constants.REPAIR_OPERATEAPPLYTYPE.equals(eqsOperateApply.getApplyType()) && Constants.AGREE_AUDIT.equals(eqsOperateApply.getAuditStatus())) {
                Integer applyNumber = eqsOperateApply.getApplyNumber();
                EntityWrapper<EqsFixture> entityWrapper_ = new EntityWrapper<>(EqsFixture.class);
                entityWrapper_.eq("code", eqsOperateApply.getFixtureCode());
                EqsFixture eqsFixture = eqsFixtureService.selectOne(entityWrapper_);
                if (eqsFixture != null) {
                    String fixtureCatalogCode = eqsFixture.getFixtureCatalogCode() == null ? "" : eqsFixture.getFixtureCatalogCode();
                    if (Constants.INEUSEFIXTURE_CATALOG.equals(fixtureCatalogCode)) {
                        eqsFixture.setInUseCount((eqsFixture.getInUseCount() == null ? 0 : eqsFixture.getInUseCount()) - applyNumber);
                    } else if (Constants.LINESPAREFIXTURE_CATALOG.equals(fixtureCatalogCode)) {
                        eqsFixture.setInLineSpareCount((eqsFixture.getInLineSpareCount() == null ? 0 : eqsFixture.getInLineSpareCount()) - applyNumber);
                    } else if (Constants.WAREHOUSEFIXTURE_CATALOG.equals(fixtureCatalogCode)) {
                        eqsFixture.setWarehouseCount((eqsFixture.getWarehouseCount() == null ? 0 : eqsFixture.getWarehouseCount()) - applyNumber);
                    } else if (Constants.REPAIRFIXTURE_CATALOG.equals(fixtureCatalogCode)) {
                        eqsFixture.setRepairCount((eqsFixture.getRepairCount() == null ? 0 : eqsFixture.getRepairCount()) - applyNumber);
                    } else {

                    }
                    int count = (eqsFixture.getRepairCount() == null ? 0 : eqsFixture.getRepairCount()) + applyNumber;
                    eqsFixture.setRepairCount(count);
                    eqsFixtureService.insertOrUpdate(eqsFixture);
                    eqsOperateApply.setAuditStatus("4");
                    eqsOperateApplyService.insertOrUpdate(eqsOperateApply);
                } else {
                    return Response.error("工装信息异常！");
                }
            } else {
                return Response.error("该申请不是维修申请或者审核未通过或者操作已完成！");
            }
        } else {
            return Response.error("申请信息异常！");
        }
        return Response.ok("操作成功");
    }

    /**
     * 报废
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "scrap", method = {RequestMethod.GET, RequestMethod.POST})
    public Response scrap(String id) {
        EntityWrapper<EqsOperateApply> entityWrapper = new EntityWrapper<>(EqsOperateApply.class);
        entityWrapper.eq("id", id);
        EqsOperateApply eqsOperateApply = eqsOperateApplyService.selectOne(entityWrapper);
        if (eqsOperateApply != null) {
            if (Constants.SCRAP_OPERATEAPPLYTYPE.equals(eqsOperateApply.getApplyType()) && Constants.AGREE_AUDIT.equals(eqsOperateApply.getAuditStatus())) {
                Integer applyNumber = eqsOperateApply.getApplyNumber();
                EntityWrapper<EqsFixture> entityWrapper_ = new EntityWrapper<>(EqsFixture.class);
                entityWrapper_.eq("code", eqsOperateApply.getFixtureCode());
                EqsFixture eqsFixture = eqsFixtureService.selectOne(entityWrapper_);
                if (eqsFixture != null) {
                    String fixtureCatalogCode = eqsFixture.getFixtureCatalogCode() == null ? "" : eqsFixture.getFixtureCatalogCode();
                    Integer count = null;
                    if (Constants.INEUSEFIXTURE_CATALOG.equals(fixtureCatalogCode)) {
                        count = (eqsFixture.getInUseCount() == null ? 0 : eqsFixture.getInUseCount()) - applyNumber;
                        eqsFixture.setInUseCount(count);
                    } else if (Constants.LINESPAREFIXTURE_CATALOG.equals(fixtureCatalogCode)) {
                        count = (eqsFixture.getInLineSpareCount() == null ? 0 : eqsFixture.getInLineSpareCount()) - applyNumber;
                        eqsFixture.setInLineSpareCount(count);
                    } else if (Constants.WAREHOUSEFIXTURE_CATALOG.equals(fixtureCatalogCode)) {
                        count = (eqsFixture.getWarehouseCount() == null ? 0 : eqsFixture.getWarehouseCount()) - applyNumber;
                        eqsFixture.setWarehouseCount(count);
                    } else if (Constants.REPAIRFIXTURE_CATALOG.equals(fixtureCatalogCode)) {
                        count = (eqsFixture.getRepairCount() == null ? 0 : eqsFixture.getRepairCount()) - applyNumber;
                        eqsFixture.setRepairCount(count);
                    } else {

                    }
                    eqsFixtureService.insertOrUpdate(eqsFixture);

                    eqsOperateApply.setAuditStatus("4");
                    eqsOperateApplyService.insertOrUpdate(eqsOperateApply);
                } else {
                    return Response.error("工装信息异常！");
                }
            } else {
                return Response.error("该申请不是报废申请或者审核未通过或者操作已完成！");
            }
        } else {
            return Response.error("申请信息异常！");
        }
        return Response.ok("操作成功");
    }


    /**
     * 审核
     *
     * @param json
     * @return
     */
    @RequestMapping(value = "auditApply", method = {RequestMethod.GET, RequestMethod.POST})
    public Response auditApply(String json,String actionCode) {
        json = StringEscapeUtils.unescapeHtml(json);
        if(Constants.AUDITSPAREOPERATEAPPLY.equals(actionCode)){
            EqsSpareOperateApply eqsSpareOperateApply = JSON.parseObject(json, EqsSpareOperateApply.class);
            eqsSpareOperateApply.setAuditDate(new Date());
            eqsSpareOperateApplyService.insertOrUpdate(eqsSpareOperateApply);
        }else if(Constants.AUDITOPERATEAPPLY.equals(actionCode)){
            EqsOperateApply eqsOperateApply = JSON.parseObject(json, EqsOperateApply.class);
            eqsOperateApply.setAuditDate(new Date());
            eqsOperateApplyService.insertOrUpdate(eqsOperateApply);
        }
        return Response.ok("操作成功");
    }


}